/**
 *添加背景
 *添加地板
 *添加精灵
 *
 */
 						//Camera的大小，可视窗口大小
var game = new Phaser.Game(800,600,Phaser.Canvas,'',{preload:preload,create:create,update:update});

function preload(){

	game.stage.backgroundColor = '#007236';
	game.load.image('sky','assets/sky.png');
	game.load.image('ground','assets/platform.png');
	game.load.image('star','assets/star.png');
	game.load.image('grass','assets/grass_1x1.png');
	game.load.image('hero','assets/hero_stopped.png');


	game.load.spritesheet('dude','assets/dude.png',32,48);

}
var platform;
var ground;
var player;
var stars;
var score = 0 ;
var scoreText;
var cursor;
var ghost;

function create(){
	//game.world.resize(6000,600);

	//game.add.image(0,0,'sky');

	
	game.world.setBounds(0,0,1920,600);
	game.add.tileSprite(0,0,1920,600,'sky');
	game.physics.startSystem(Phaser.Physics.ARCADE);
	//img.scale.set(2);
	//img.width = game.world.width;
	//img.height = game.world.height;
	
	//game.forceSingleUpdate = true;

	platform = game.add.group();
	platform.enableBody = true;

	ground = platform.create(0,game.world.height-64,'ground');
	ground.scale.set(5,2);
	ground.body.immovable = true;

	var limit1 = platform.create(200,game.world.height-106,'grass');
	limit1.body.immovable = true;
	var limit2 = platform.create(600,game.world.height-106,'grass');
	limit2.body.immovable = true;

	var ledge1 = platform.create(-150,250,'ground');
	ledge1.body.immovable = true;

	var ledge2 = platform.create(400,400,'ground');
	ledge2.body.immovable = true;

	player = game.add.sprite(32,400,'dude');

	//game.physics.enable(player,game.physics.ARCADE);
	//game.physics.enable(player);
	game.physics.arcade.enable(player);

	player.body.bounce.y = 0.2;
	player.body.gravity.y = 400;
	//player.body.collideWorldBounds = true;

	player.animations.add('left',[0,1,2,3],10,true);
	player.animations.add('right',[5,6,7,8],10,true);
	player.animations.frame = 4;

	ghost = game.add.sprite(400,game.world.height-106,'hero');
	game.physics.arcade.enable(ghost);
	ghost.body.velocity.x = 150;

	//ledge1.fixedToCamera = true;  //相对于窗口即Camera不动
	game.camera.follow(player);	  //走到中心就要移动背景及world

	/*stars = game.add.group();
	stars.enableBody = true;

	for(var i = 0; i<12; ++i){
		var star = stars.create(i * 70,0,'star');
		star.body.gravity.y = 300;
		star.body.bounce.y = 0.7 + Math.random() * 0.2;
	}*/

	scoreText = game.add.text(16,16,'score:0',{fontSize:'32px',fill:'#000000'});

	cursor = game.input.keyboard.createCursorKeys();
}
function update(){
	game.physics.arcade.collide(player,platform);
	game.physics.arcade.overlap(player,ghost,death,null,this);
	//game.physics.arcade.collide(stars,platform);
	game.physics.arcade.overlap(player,stars,collectStars,null,this);

	if(ghost.body.x>=600-42||ghost.body.x<=200+42){
		ghost.body.velocity.x = -1 * ghost.body.velocity.x;
	}
	player.body.velocity.x = 0;

	if(cursor.left.isDown){
		player.body.velocity.x = -150;
		player.animations.play('left');
	}else if(cursor.right.isDown){
		player.body.velocity.x = 150;
		player.animations.play('right');
	}else{
		player.animations.stop();
		player.frame = 4;
	}
	if(cursor.up.isDown && player.body.touching.down){
		player.body.velocity.y = -350;
	}
}
function render(){

}
function collectStars(player,star){
	star.kill();
	score += 10;
	scoreText.text = 'score:' + score;
}
function death(player,ghost){
	player.kill();
}
function fantan(ghost,platform){
	/*if(ghost.body.velocity.x < 0){
		ghost.body.velocity.x = 150;
	}else{
		ghost.body.velocity.x = -150;
	}*/
}